package com.orangelife.mobile.goodsrent.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.util.OrangePhoneUtil;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.ImageDownloader;
import com.curry.android.view.BadgeView;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.application.ExitApplication;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.goodsrent.biz.GoodsInfoCallBack;
import com.orangelife.mobile.goodsrent.biz.GoodsListImpl;
import com.orangelife.mobile.goodsrent.biz.GoodsRentBiz;
import com.orangelife.mobile.goodsrent.dao.GoodsDao;
import com.orangelife.mobile.goodsrent.entity.GoodsInfo;
import com.orangelife.mobile.surrounding.activity.SelectedStandardActivity;
import com.thirdsdk.umeng.UmShareBiz;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.GoodsRentPopupWindow;
import com.orangelife.mobile.web.WebWin;
import com.umeng.socialize.media.UMImage;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class RentDetailActivity extends GoodsListImpl {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_call)
    ImageView ivTitleCall;
    @Bind(R.id.flShoppingCart)
    FrameLayout flShoppingCart;
    @Bind(R.id.ivBigPhoto)
    ImageView ivBigPhoto;
    @Bind(R.id.tvGoodsName)
    TextView tvGoodsName;
    @Bind(R.id.ibShare)
    ImageButton ibShare;
    @Bind(R.id.tvSelectStandard)
    TextView tvSelectStandard;
    @Bind(R.id.tvGoodsDetail)
    TextView tvGoodsDetail;
    @Bind(R.id.wwPhoto)
    WebWin wwPhoto;
    @Bind(R.id.ibShoppingCard)
    ImageButton ibShoppingCard;
    @Bind(R.id.btnParticipate)
    Button btnParticipate;
    @Bind(R.id.rlPay)
    RelativeLayout rlPay;
    @Bind(R.id.tvTotalPrice)
    TextView tvTotalPrice;

    /**
     * 分享图片
     */
    @SuppressWarnings("unused")
    private UMImage umengImage;
    /**
     * 分享title
     */
    private String uMtitle = "";
    /**
     * 分享content
     */
    private String uMcontent = "";

    private List<Map<String, Object>> listSummary = null;

    private List<Map<String, Object>> listProduct = null;

    /**
     * 图片下载
     */
    private ImageDownloader image = ImageDownloader.getImageDownloader();

    private String goodsID = "";

    private String dispatchType = "";

    private Dialog dialog;
    /**
     * 请求地址
     */
    private String url = "";

    private BadgeView mBadgeView;

    private GoodsRentPopupWindow goodsRentPopupWindow;
    /** 租借物品数量 */
    private List<GoodsInfo> goodsList = null;

    private GoodsInfoCallBack mGoodsInfoCallBack;

    private GoodsInfo goodsInfo;
    UmShareBiz shareBiz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rent_detail);
        ButterKnife.bind(this);
        shareBiz = new UmShareBiz(this);
        findView();
    }

    /***
     * 赋值
     */
    private void findView() {
        mGoodsInfoCallBack = this;
        Intent intent = getIntent();
        goodsInfo = intent.getParcelableExtra("detail");
        flShoppingCart.setVisibility(View.GONE);
        tvTitle.setText("租借详情");
        goodsID = intent.getStringExtra("goodsID");
        ivTitleCall.setOnClickListener(listener);
        tvSelectStandard.setOnClickListener(listener);
        btnParticipate.setOnClickListener(listener);
        ibShoppingCard.setOnClickListener(listener);
        ibShare.setOnClickListener(listener);
//        map = (Map<String, Object>) intent.getSerializableExtra("detail");
        /*getDataFromServer();
        getActivityDetail();*/
        setDetail();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getGoodsNumber() >= 0) {
            setShoppingCardNum(ibShoppingCard, getGoodsNumber());
        }
        tvTotalPrice.setText(Constant.RMB_FLAG + GoodsDao.getInstance().getGoodsTotalPrice());
    }

    /**
     * 下拉刷新的进度条
     */
    private void getDataFromServer() {
        dialog = DialogHelper.getInstance().createLoadingDialog(this,
                Constant.LOADING);
        dialog.show();
    }

    /**
     * 获得详情
     */
    @SuppressWarnings("deprecation")
    private void getActivityDetail() {
        url = Constant.URL_GOODRENT_DETAIL;
        HashMap<String, String> mapSubmit = new HashMap<>();
        Intent intent = getIntent();
        if (!StringUtil.isBlank(goodsID)) {
            mapSubmit.put("{goodsID}", goodsID);
        } else if (!StringUtil.isBlank(intent.getStringExtra("id"))) {
            mapSubmit.put("{goodsID}", intent.getStringExtra("id"));
        }
        GoodsRentBiz.getInstance().requestGet(mapSubmit, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                DialogHelper.closeDialog(dialog);
//                mapDetail = GoodsRentBiz.getInstance().handleOrderDetail(response, errCode);
                setDetail();
            }
        }, url);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_title_call:
                    //打电话
                    OrangePhoneUtil.callCommProperty(RentDetailActivity.this);
                    break;
                case R.id.ibShare:
                    shareBiz.share(uMtitle, uMcontent, Constant.HOTGOODS_SHARE_URL, goodsID);
                    break;
                case R.id.tvSelectStandard:
                    if (goodsInfo != null) {
                        ExitApplication.getInstance().addFailActivity(RentDetailActivity.this);
                        Intent intent = new Intent();
                        intent.putExtra("flag", 1);
                        intent.putExtra("isRent", true);
//                        mapDetail.put("dispatchType",dispatchType);
                        intent.putExtra("specification", goodsInfo);
                        intent.putExtra("position", 0);
                        intent.setClass(RentDetailActivity.this,
                                SelectedStandardActivity.class);
                        startActivity(intent);
                    }
                    break;
                case R.id.btnParticipate:
                    jumpNewActivity();
                    break;
                case R.id.ibShoppingCard:
                    //弹出popupwindow
                    goodsList = GoodsDao.getInstance().selectUserInfoList();
                    if (goodsList != null && goodsList.size() != 0) {
                        showPopupWindow(rlPay);
                    }
                    break;
                default:
                    break;
            }

        }
    };

    /**
     * 处理返回的信息数据
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 500:
                    DialogHelper.closeDialog(dialog);
                    ToastHelper.getInstance()._toast(msg.obj.toString());
                    break;
                case 1011:
                    isLogin(dialog, RentDetailActivity.this);
                    break;
                default:
                    break;
            }
        }

    };

    /***
     * 设置详情
     */
    private void setDetail() {

        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = width * 11 / 16;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
        ivBigPhoto.setLayoutParams(params);
        goodsID = goodsInfo.getrGoodID();
        uMtitle = goodsInfo.getGoodName();
        uMcontent = goodsInfo.getGoodsBrief();
        image.download(goodsInfo.getGoodsLogo().trim(), ivBigPhoto,
                ImageDownloader.ImageSize.IMAGE_SIZE_MIDDLE_JPG);
        tvGoodsName.setText(goodsInfo.getGoodName());
//        tvCommercialTnantName.setText(mapDetail.get("merchantName").toString());
//        String summary = mapDetail.get("summary").toString();
       /* if (!StringUtil.isBlank(summary)) {
            listSummary = (List<Map<String, Object>>) mapDetail.get("summary");
            ActivityDetailListViewAdapter adapter = new ActivityDetailListViewAdapter(
                    this, listSummary);
            lvConsumerTips.setAdapter(adapter);
        }*/
        tvGoodsDetail.setText(goodsInfo.getGoodsBrief());
//        listProduct = (List<Map<String, Object>>) mapDetail.get("products");
        setWebView();
        tvTotalPrice.setText(GoodsDao.getInstance().getGoodsTotalPrice());
        if (getGoodsNumber() > 0) {
            setShoppingCardNum(ibShoppingCard, getGoodsNumber());
        }
    }

    /**
     * 获取物品数量
     *
     * @return
     */
    private int getGoodsNumber() {
        return GoodsDao.getInstance().getGoodsNum();
    }

    /**
     * 设置照片的webview
     */
    private void setWebView() {
        String pics = goodsInfo.getGoodImgs();
        wwPhoto.loadDataWithBaseURL(pics);
    }

    @Override
    public void startShopCardAnim(View view) {
    }

    @Override
    public void getGoodsRentNum(int num) {
        setShoppingCardNum(ibShoppingCard, getGoodsNumber());
    }

    @Override
    public void getGoodsTotalPrice(String totalPrice) {
        setGoodsTotalPrice(tvTotalPrice, totalPrice);
    }
}
